﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Jinjia.Approve.IRepository;
using Jinjia.Approve.IRepository.JJFlow;
using Jinjia.Approve.IService.JJFlow.Approvers;
using JinJia.Core.Base.Exceptions;

namespace Jinjia.Approve.Service.JJFlow.Approvers
{
    public class GoodsSampleTradeApproverRealize : IJJApprovers
    {
        private readonly IJJApproversRepository _jjApproversRepository;
        private readonly IErpaUserRepository _erpaUserRepository;

        public GoodsSampleTradeApproverRealize(IJJApproversRepository jjApproversRepository, IErpaUserRepository erpaUserRepository)
        {
            _jjApproversRepository = jjApproversRepository;
            _erpaUserRepository = erpaUserRepository;
        }

        public async Task<List<int>> GetInstanceNodeEmpInfo(string biz_order_id, int start_user = 0, string depart_id = "0", Dictionary<string, string> dic = null)
        {
            var member_list = await _jjApproversRepository.GetOrgMemberList(biz_order_id);
            if (member_list.Count == 0)
            {
                throw new BusinessException("该数据不存在！");
            }

            var external_user_list = member_list.Select(x => x.external_user_id).ToList();
            var user_list = await _erpaUserRepository.GetUsersByDingTalkUserIds(external_user_list);
            var ding_user_list = user_list.Select(x => x.ding_user_id).ToList();

            var msg = "";
            var except = external_user_list.Except(ding_user_list).ToList();
            foreach (var item in except)
            {
                var member = member_list.FirstOrDefault(x => x.external_user_id == item);
                if (member != null)
                {
                    msg += member.member_name + "个人信息不全;";
                }
            }

            if (!string.IsNullOrWhiteSpace(msg))
            {
                throw new BusinessException(msg);
            }

            return user_list.Select(x => x.id).ToList();
        }
    }
}